// pages/recruit/personal/mytask/mytask.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hb_show: "",
    posterurl: "",
    pageindex: 1,
    tasklist: [],
    dialogShow: false,
    buttons: [{ text: '取消' }, { text: '确定' }],
    dialogtext: "",
    btntype: '',//按钮属性是取消报名还是删除记录
    deleteid: '',//被操作的兼职的id
    posterConfig: {},
    failSave: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    getApp().globalData.get("/api/user/mine_task", {}).then(res => {
      console.log(res)
    })
    wx.getSystemInfo({
      success(res) {
        let boxwidth = res.screenWidth;
        let bgheight = boxwidth*1.46;
        that.data.posterConfig.width = boxwidth;
        that.data.posterConfig.height = boxwidth*1.89;
        that.data.posterConfig.backgroundColor= '#fff';
        that.data.posterConfig.images = [
          {
            x: 0,
            y: 0,
            preload: true,
            url: "https://www.yuzeng.top/hb.jpg",
            width: boxwidth,
            height: bgheight
          },
          {
            x: (boxwidth - 100) / 2,
            y: bgheight*0.45-50,
            preload: true,
            url: "https://www.yuzeng.top/head.jpg",
            width: 100,
            height: 100,
            borderRadius: 100,
          },
          {
            x: (boxwidth - 100) / 2,
            y: bgheight + 20,
            preload: true,
            url: "https://www.yuzeng.top/erweima.jpg",
            width: 100,
            height: 100,
            borderRadius: 100,
          }
        ]
        that.data.posterConfig.texts =[
          {
            x: boxwidth/2,
            y: bgheight * 0.45+100,
            text: "招募星座运势文案编辑",
            fontSize: 28,
            height: 60,
            textAlign: "center",
            color: "#ff5e00"
          },
          {
            x: boxwidth / 2-70,
            y: bgheight * 0.45 + 150,
            text: "文案编辑|20元一次",
            fontSize: 24,
            height: 60,
            textAlign: "left",
            color: "#ff5e00"
          },
          {
            x: boxwidth / 2-70,
            y: bgheight * 0.45 + 180,
            text: "南京|鼓楼",
            fontSize: 24,
            height: 60,
            textAlign: "left",
            color: "#ff5e00"
          },
          {
            x: boxwidth / 2 ,
            y: bgheight + 140,
            text: "扫码投递",
            fontSize: 18,
            height: 60,
            textAlign: "center"
          }
        ]
        that.setData({
          posterConfig: that.data.posterConfig
        })
        console.log(that.data.posterConfig)
        
      }
    })
    if (JSON.stringify(options) != "{}") {
      that.setData({
        pageindex: options.id
      })
    }
    /**
     * 1、表示已接受的任务。2、我发布的兼职。3、正在招聘的已接任务或发布的兼职。4、已经截止的已接任务或发布的兼职
     */
    let res = [
      {
        state: 1,
        type: "task",
        jobid: "124541",
        title: "我接受的一个任务11111",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      },
      {
        state: 1,
        type: "task",
        jobid: "124542",
        title: "11月1-我接受的一个任务2222",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      },
      {
        state: 1,
        type: "task",
        jobid: "124531",
        title: "我接受的一个任务3333",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      },
      {
        state: 2,
        type: "job",
        jobid: "124543",
        title: "我发布的兼职1111",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      },
      {
        state: 2,
        type: "job",
        jobid: "124543",
        title: "我发布的兼职2222",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      },
      {
        state: 3,
        type: "task",
        jobid: "124544",
        title: "我接受的任务正在招聘11111",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      },
      {
        state: 3,
        type: "job",
        jobid: "124545",
        title: "我发布的兼职正在招聘11111",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      },
      {
        state: 4,
        type: "task",
        jobid: "124545",
        title: "我接受的任务已经结束啦",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      }
      ,
      {
        state: 4,
        type: "job",
        jobid: "124545",
        title: "我发布的兼职任务招人到期啦",
        workplace: "南京浦口区xx位置",
        worktime: "2019-10-21",
        pay: "110/天",
        comtel: 17755015130
      }
    ]
    let tasklist = that.sorttasklist(res);
    that.setData({
      tasklist: tasklist
    })
  },
  sorttasklist: function (data) {
    let arr1 = [], arr2 = [], arr3 = [], arr4 = [], arr5 = [];
    for (let i in data) {
      if (data[i].state == 3) {
        arr1.push(data[i])
      } else if (data[i].state == 2) {
        arr2.push(data[i])
      } else if (data[i].state == 1) {
        arr3.push(data[i])
      } else if (data[i].state == 4) {
        arr4.push(data[i])
      } else if (data[i].state == 0) {
        arr5.push(data[i])
      }
    }
    return [...arr1, ...arr2, ...arr3, ...arr4, ...arr5];
  },
  changeindex: function (e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      pageindex: index
    })
  },
  mytaskshare: function (e) {
    console.log("我去查看报名信息啦！")
    wx.navigateTo({
      url: './mytaskdetails/mytaskdetails?jobid=' + e.currentTarget.dataset.jobid,
    })
  },
  cancleenter: function(e){//取消发布
    console.log(e.currentTarget.dataset.jobid)
    const that = this;
    let dialogtext = "是否取消发布该职位";
    let jobid = e.currentTarget.dataset.jobid;
    that.setData({
      dialogShow: true,
      dialogtext: dialogtext,
      deleteid: jobid
    })
  },
  tapDialogButton:function(e){
    const that = this;
    let type = e.detail.index;
    if (type == 0) {
      that.setData({
        dialogShow: false,
      })
    } else {
      that.deletetype()
    }
  },
  deletetype: function () {//确定按钮的后续操作
    const that = this;
    let type = that.data.btntype;
    let deleteid = that.data.deleteid;
    //发送请求取消正在审核的兼职
    console.log(deleteid)
    console.log("取消发布的兼职")
    that.setData({//操作成功后再删除
      dialogShow: false,
    })
  },
  checkmyjob: function(e){//根据id去查看报名的人数
    console.log("我去查看报名信息啦！")
    wx.navigateTo({
      url: './mytaskdetails/mytaskdetails?jobid='+e.currentTarget.dataset.jobid,
    })
  },
  rewire: function (e) {//根据去重新发布兼职
    wx.navigateTo({
      url: '/pages/release/index/index?jobid=' + e.currentTarget.dataset.jobid,
    })
  },
  checkbaoming: function (e){
    wx.navigateTo({
      url: './mytasksign/mytasksign?jobid=' + e.currentTarget.dataset.jobid,
    })
  },
  onPosterSuccess:function(e){
    const that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.writePhotosAlbum"] != undefined) {
          if (res.authSetting["scope.writePhotosAlbum"]) {
            that.setData({
              failSave: false
            })
          } else {
            wx.showToast({ icon: 'none', title: '请点击授权使用相册', duration: 1500 });
            that.setData({
              failSave: true
            })
          }
        }
      }
    })
    this.setData({
      posterurl: e.detail,
      hb_show: "show"
    })
  },
  hidehaibao: function(e) {
    this.setData({
      hb_show: ""
    })
  },
  saveimg: function(e){//缺少指定的id
    const that = this;
    wx.showLoading({ mask: true, title: '保存中' });
    wx.saveImageToPhotosAlbum({
      filePath: e.currentTarget.dataset.imgsrc,
      success(res) {
        wx.hideLoading();
        that.setData({
          hb_show: ""
        })
      },
      fail(){
        wx.showToast({ icon: 'none', title: '保存失败,请在设置里打开', duration: 1500});
        that.setData({
          failSave: true
        })
      }
    })
  }
})